<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/7/2
 * Time: 11:00
 */

namespace Shop\Api;
use Shop\Model\OrdersModel;
use Shop\Model\OrderStatusHistoryModel;

/**
 * 订单状态变更接口
 * Class OrdersStatusApi
 * @package Shop\Api
 */
class OrderStatusApi{

    /**
     * 待支付－》已支付变更
     * 1. 检测支付状态
     */
    const PAY_ORDER = "Shop/OrderStatus/payOrder";

    /**
     * 待支付－》货到付款状态变更
     */
    const CASH_ON_DELIVERY = "Shop/OrderStatus/cashOndelivery";
    /**
     * 待发货－》已发货状态变更
     */
    const SHIPPED = "Shop/OrderStatus/shipped";
    /**
     * 待确认－》已确认状态变更
     */
    const CONFIRM_ORDER = "Shop/OrderStatus/confirmOrder";
    /**
     * 待确认－》取消状态变更
     */
    const BACK_ORDER = "Shop/OrderStatus/backOrder";
    /**
     * 已发货 -> 已收货 状态变更
     */
    const CONFIRM_RECEIVE = "Shop/OrderStatus/confirmReceive";
    /**
     * 已收货 －》退货 状态变更
     */
    const RETURNED = "Shop/OrderStatus/returned";
    /**
     * 订单评价－》完成 状态变更
     */
    const EVALUATION = "Shop/OrderStatus/evaluation";

    /**
     * 订单评价－》取消 状态变更
     */
    const CANCEL = "Shop/OrderStatus/cancelOrder";

    //****以下作为自动调用，请不要使用下面接口，除非你明确知道自己干嘛
    //****有疑问找@author hebiduhebi@126.COM
	
	/**
	 * 更新状态=》已取消
	 */
	const ORDER_STATUS_TO_CANCEL = "Shop/OrderStatus/orderStatusToCancel";
	
	/**
	 * 更新状态=》已收货
	 */
	const ORDER_STATUS_TO_RECIEVED = "Shop/OrderStatus/orderStatusToRecieved";
	
	/**
	 * 更新状态=》已完成
	 */
	const ORDER_STATUS_TO_COMPLETED = "Shop/OrderStatus/orderStatusToCompleted";

    /**
     * 更新状态=》已评价
     */
	const ORDER_STATUS_TO_AUTO_EVALUATION="Shop/OrderStatus/toAutoEvaluation";
	

    /**
     *
     */
    function __construct(){
        $this->model = new OrdersModel();
    }

    /**
     * 订单支付－货到付款操作
     * @param $ids  订单id数组
     * @return array
     */
//    public function cashOndelivery($ids,$isauto,$uid){
//        $orderStatusHistoryModel = new OrderStatusHistoryModel();
//        //
//        foreach($ids as  $id){
//
//            $result = $this->model->where(array('id'=>$id))->find();
//
//            if($result == false){
//                return $this->returnErr($this->model->getDbError());
//            }
//
//            if(is_null($result)){
//                return $this->returnErr("订单ID错误!");
//            }
//
//            if($result['pay_status'] !=  OrdersModel::ORDER_TOBE_PAID){
//                return $this->returnErr("当前订单状态无法变更！");
//            }
//			//dump($result['orderid']);
//            $entity = array(
//                'reason'=>"用户选择货到付款支付!",
//                'order_code'=>$result['order_code'],
//                'operator'=>$uid,
//                'status_type'=>'PAY',
//                'cur_status'=>$result['pay_status'],
//                'isauto'=>0,
//                'next_status'=> OrdersModel::ORDER_CASH_ON_DELIVERY,
//            );
//
//            $this->model->startTrans();
//            $flag = true;
//            $return = "";
//
//            //设置订单状态为货到付款
//            $result = $this->model->where(array('id'=>$id))->save(array('pay_status'=> OrdersModel::ORDER_CASH_ON_DELIVERY));
//            if($result === false){
//                $flag = false;
//                $return = $this->model->getDbError();
//            }
//            if($result == 0){
//                $flag = false;
//                $return = "订单ID有问题!";
//            }
//
//            if($orderStatusHistoryModel->create($entity,1)){
//                $result = $orderStatusHistoryModel->add();
//                if($result === false){
//                    $flag = false;
//                    $return = $orderStatusHistoryModel->getDbError();
//                }
//            }else{
//                $flag = false;
//                $return = $orderStatusHistoryModel->getError();
//            }
//            //单个
//        }
//
//        if($flag){
//            $this->model->commit();
//            return $this->returnSuc($return);
//        }else{
//            $this->model->rollback();
//            return $this->returnErr($return);
//        }
//
//    }

	/**
	 * 返回错误结构
	 * @return array('status'=>boolean,'info'=>Object)
	 */
	protected function returnErr($info) {
		return array('status' => false, 'info' => $info);
	}
	
	/**
	 * 返回成功结构
	 * @return array('status'=>boolean,'info'=>Object)
	 */
	protected function returnSuc($info) {
		return array('status' => true, 'info' => $info);
	}


    /**
     * 订单发货操作
     * @param $order_code
     * @param $uid
     * @return array
     * @internal param $id
     * @internal param $isauto
     */
    public function shipped($order_code,$uid){
        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        $result = $this->model->where(array('order_code'=>$order_code))->find();

        if($result == false){
            return $this->returnErr($this->model->getDbError());
        }

        if(is_null($result)){
            return $this->returnErr("订单ID错误!");
        }

        if($result['order_status'] != OrdersModel::ORDER_TOBE_SHIPPED){
            return $this->returnErr("当前订单状态无法变更！");
        }

        $entity = array(
            'reason'=>"订单发货操作!",
            'order_code'=>$result['order_code'],
            'operator'=>$uid,
            'status_type'=>'ORDER',
            'isauto'=>0,
            'cur_status'=>$result['order_status'],
            'next_status'=> OrdersModel::ORDER_SHIPPED,
        );

        $this->model->startTrans();
        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order_code))->save(array('order_status'=> OrdersModel::ORDER_SHIPPED));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }
        if($result == 0){
            $flag = false;
            $return = "订单ID有问题!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }



        if($flag){
            $this->model->commit();
            return $this->returnSuc($return);
        }else{
            $this->model->rollback();
            return $this->returnErr($return);
        }

    }


    /**
     * 订单确认
     * @param $order_code
     * @param $is_auto
     * @param $uid
     * @return array
     */
    public function confirmOrder($order_code,$is_auto,$uid){
        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        $result = $this->model->where(array('order_code'=>$order_code))->find();

        if($result == false){
            return $this->returnErr($this->model->getDbError());
        }

        if(is_null($result)){
            return $this->returnErr("订单ID错误!");
        }

        if($result['order_status'] != OrdersModel::ORDER_TOBE_CONFIRMED){
            return $this->returnErr("当前订单状态无法变更！");
        }

        $entity = array(
            'reason'=>"订单确认操作!",
            'order_code'=>$result['order_code'],
            'operator'=>$uid,
            'status_type'=>'ORDER',
            'isauto'=>0,
            'cur_status'=>$result['order_status'],
            'next_status'=> OrdersModel::ORDER_TOBE_SHIPPED,
        );

        $this->model->startTrans();
        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order_code))->save(array('order_status'=> OrdersModel::ORDER_TOBE_SHIPPED));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }
        if($result == 0){
            $flag = false;
            $return = "订单ID有问题!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }



        if($flag){
        	//订单确认库存减少
            $this->model->commit();
            return $this->returnSuc($return);
        }else{
            $this->model->rollback();
            return $this->returnErr($return);
        }

    }

    /**
     * 退回订单
     * @param $order_code
     * @param $reason
     * @param $isauto
     * @param $uid
     * @return array
     */
    public function backOrder($order_code,$reason,$isauto,$uid){

        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        $result = $this->model->where(array('order_code'=>$order_code))->find();

        if($result == false){
            return $this->returnErr($this->model->getDbError());
        }

        if(is_null($result)){
            return $this->returnErr("订单ID错误!");
        }

        if($result['order_status'] != OrdersModel::ORDER_TOBE_CONFIRMED){
            return $this->returnErr("当前订单状态无法变更！");
        }

        $entity = array(
            'reason'=>$reason,
            'order_code'=>$result['order_code'],
            'operator'=>$uid,
            'status_type'=>'ORDER',
            'isauto'=>0,
            'cur_status'=>$result['order_status'],
            'next_status'=> OrdersModel::ORDER_BACK,
        );

        $this->model->startTrans();
        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order_code))->save(array('order_status'=>OrdersModel::ORDER_BACK));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }
        if($result == 0){
            $flag = false;
            $return = "订单ID有问题!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }



        if($flag){
            $this->model->commit();
            return $this->returnSuc($return);
        }else{
            $this->model->rollback();
            return $this->returnErr($return);
        }


    }

    /**
     * 确认收货操作
     * @param $order_code
     * @param $isauto
     * @param $uid
     * @return array
     */
    public function confirmReceive($order_code,$isauto,$uid){
        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        $result = $this->model->where(array('order_code'=>$order_code))->find();

        if($result == false){
            return $this->returnErr($this->model->getDbError());
        }

        if(is_null($result)){
            return $this->returnErr("订单ID错误!");
        }

        if($result['order_status'] != OrdersModel::ORDER_SHIPPED){
            return $this->returnErr("当前订单状态出错!");
        }

        $entity = array(
            'reason'=>"确认收货操作!",
            'order_code'=>$result['order_code'],
            'operator'=>$uid,
            'isauto'=>0,
            'status_type'=>'ORDER',
            'cur_status'=>$result['order_status'],
            'next_status'=>OrdersModel::ORDER_RECEIPT_OF_GOODS,
        );

        $this->model->startTrans();
        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order_code))->save(array('order_status'=>OrdersModel::ORDER_RECEIPT_OF_GOODS));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }
        if($result == 0){
            $flag = false;
            $return = "订单ID有问题!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }



        if($flag){
            $this->model->commit();
            return $this->returnSuc($return);
        }else{
            $this->model->rollback();
            return $this->returnErr($return);
        }
    }

    /**
     * 退货操作
     * @param $order_code
     * @param $isauto
     * @param $uid
     * @return array
     * @internal param $id
     */
    public function returned($order_code,$isauto,$uid){
        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        $result = $this->model->where(array('order_code'=>$order_code))->find();

        if($result == false){
            return $this->returnErr($this->model->getDbError());
        }

        if(is_null($result)){
            return $this->returnErr("订单ID错误!");
        }

        if($result['order_status'] == OrdersModel::ORDER_RECEIPT_OF_GOODS ){
            return $this->returnErr("当前订单状态出错!");
        }

        $entity = array(
            'reason'=>"订单退货操作!",
            'order_code'=>$result['order_code'],
            'operator'=>$uid,
            'status_type'=>'ORDER',
            'isauto'=>$isauto,
            'cur_status'=>$result['order_status'],
            'next_status'=>OrdersModel::ORDER_RETURNED,
        );

        $this->model->startTrans();
        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order_code))->save(array('order_status'=>OrdersModel::ORDER_RETURNED));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }

        if($result == 0){
            $flag = false;
            $return = "订单ID有问题!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }



        if($flag){
            $this->model->commit();
            return $this->returnSuc($return);
        }else{
            $this->model->rollback();
            return $this->returnErr($return);
        }
    }

    /**
     * 订单评价操作
     * @param $id
     * @param $isauto
     * @param $uid
     * @return array
     */
    public function evaluation($order_code,$isauto,$uid){
        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        $result = $this->model->where(array('order_code'=>$order_code))->find();

        if($result == false){
            return $this->returnErr($this->model->getDbError());
        }

        if(is_null($result)){
            return $this->returnErr("订单ID错误!");
        }

        if($result['order_status'] != OrdersModel::ORDER_RECEIPT_OF_GOODS ){
            return $this->returnErr("当前订单状态出错!");
        }

        $entity = array(
            'reason'=>"订单评价操作!",
            'order_code'=>$result['order_code'],
            'operator'=>$uid,
            'status_type'=>'COMMENT',
            'isauto'=>0,
            'cur_status'=>$result['order_status'],
            'next_status'=>OrdersModel::ORDER_COMPLETED,
        );

        $this->model->startTrans();
        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order_code))->save(array('comment_status'=>OrdersModel::ORDER_HUMAN_EVALUATED,'order_status'=>OrdersModel::ORDER_COMPLETED));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }

        if($result == 0){
            $flag = false;
            $return = "订单ID有问题!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }



        if($flag){
            $this->model->commit();
            return $this->returnSuc($return);
        }else{
            $this->model->rollback();
            return $this->returnErr($return);
        }
    }


    /**
     * 支付状态变更
     * 未支付-》已支付
     * @param string $trade_no  交易号（本地商户）
     * @param string $order_code 订单编号
     * @param string $uid  用户ID
     * @param string $pay_type
     * @return array|mixed
     */
    public function payOrder($trade_no,$order_code,$uid,$pay_type){
        if(empty($order_code)){
            return $this->returnErr("订单ID不能为空!");
        }

        $map=array(
            'order_code'=>$order_code,
        );

        $result = apiCall(OrdersApi::GET_INFO,array($map));

        if(!$result['status']){
            return $result;
        }
        if(is_null($result['info'])){
            LogRecord("订单ID非法",__FILE__.__LINE__,'INFO');
            return $this->returnErr("订单ID非法!");
        }
        //0. 检测订单状态，防止重复修改
        $order = $result['info'];
        // 只有订单的支付状态为 未支付时才能继续执行，否则返回
        if($order['pay_status'] != OrdersModel::ORDER_TOBE_PAID){

            return $this->returnErr("该订单".$order_code."状态已变更为支付状态!");
        }

        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        $this->model->startTrans();
        $flag = true;
        $return = "";

        $entity = array(
            'reason'=>"订单支付操作!",
            'order_code'=>$order_code,
            'operator'=>$uid,
            'status_type'=>'PAY',
            'isauto'=>0,
            'cur_status'=>$order['pay_status'],
            'next_status'=>OrdersModel::ORDER_PAID,
        );
        //1. 保存本地交易号，与修改订单状态
        $result = $this->model->where($map)->save(array('pay_type'=>$pay_type,'pay_code'=>$trade_no,'pay_status'=>OrdersModel::ORDER_PAID));

        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }

        if($result == 0){
            $flag = false;
            $return = "订单ID有问题!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }


        if($flag){
            $this->model->commit();
            return $this->returnSuc($return);
        }else{
            $this->model->rollback();
            return $this->returnErr($return);
        }


    }


    /**
     * 订单取消操作
     * @param $order_code
     * @param $isauto
     * @param $uid
     * @return array
     * @internal param $id
     */
    public function cancelOrder($order_code,$isauto,$uid){
        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        $result = $this->model->where(array('order_code'=>$order_code))->find();

        if($result == false){
            return $this->returnErr($this->model->getDbError());
        }

        if(is_null($result)){
            return $this->returnErr("订单ID错误!");
        }

        if($result['pay_status'] != OrdersModel::ORDER_TOBE_PAID ){
            return $this->returnErr("已支付订单，无法取消!");
        }

        if($result['order_status'] != OrdersModel::ORDER_TOBE_CONFIRMED ){
            return $this->returnErr("当前订单状态出错!");
        }

        $entity = array(
            'reason'=>"用户取消了订单!",
            'order_code'=>$result['order_code'],
            'operator'=>$uid,
            'status_type'=>'ORDER',
            'isauto'=>$isauto,
            'cur_status'=>$result['order_status'],
            'next_status'=>OrdersModel::ORDER_CANCEL,
        );

        $this->model->startTrans();
        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order_code))->save(array('order_status'=>OrdersModel::ORDER_CANCEL));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }

        if($result == 0){
            $flag = false;
            $return = "订单ID有问题，因为订单未被更新!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }



        if($flag){
            $this->model->commit();
            return $this->returnSuc($return);
        }else{
            $this->model->rollback();
            return $this->returnErr($return);
        }
    }


    /****************************以下自动调用内容*******************************/

    /**
     * 系统取消订单操作
     * @param $order
     * @return array
     * @internal param $id
     */
    private function sysCancelOrder($order){
        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        if($order['pay_status'] != OrdersModel::ORDER_TOBE_PAID ){
            return $this->returnErr("已支付订单，无法取消!");
        }

        if($order['order_status'] != OrdersModel::ORDER_TOBE_CONFIRMED ){
            return $this->returnErr("当前订单状态不为待确认!");
        }

        $entity = array(
            'reason'=>"系统取消了订单!",
            'order_code'=>$order['order_code'],
            'operator'=>0,
            'status_type'=>'ORDER',
            'isauto'=>1,
            'cur_status'=>$order['order_status'],
            'next_status'=>OrdersModel::ORDER_CANCEL,
        );

        $flag = true;
        $return =  "";
        $result = $this->model->where(array('order_code'=>$order['order_code']))->save(array('order_status'=>OrdersModel::ORDER_CANCEL));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }

        if($result == 0){
            $flag = false;
            $return = "订单ID有问题，因为订单未被更新!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }


        if($flag){
            return $this->returnSuc($return);
        }else{
            return $this->returnErr($return);
        }
    }

    /**
     * 系统自动收货订单操作
     * @param $order
     * @return array
     * @internal param $id
     */
    private function sysRecivedOrder($order){
        $orderStatusHistoryModel = new OrderStatusHistoryModel();

        if($order['order_status'] != OrdersModel::ORDER_SHIPPED){
            return $this->returnErr("当前订单状态出错!");
        }

        $entity = array(
            'reason'=>"系统超时自动确认收货操作!",
            'order_code'=>$order['order_code'],
            'operator'=>0,
            'isauto'=>1,
            'status_type'=>'ORDER',
            'cur_status'=>$order['order_status'],
            'next_status'=>OrdersModel::ORDER_RECEIPT_OF_GOODS,
        );

        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order['order_code']))->save(array('order_status'=>OrdersModel::ORDER_RECEIPT_OF_GOODS));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }
        if($result == 0){
            $flag = false;
            $return = "订单ID有问题!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }



        if($flag){
            return $this->returnSuc($return);
        }else{
            return $this->returnErr($return);
        }
    }


    /**
     * 系统自动关闭订单操作
     * @param $order
     * @return array
     * @internal param $id
     */
    private function sysCompleteOrder($order){
        $orderStatusHistoryModel = new OrderStatusHistoryModel();

        if($order['order_status'] != OrdersModel::ORDER_RECEIPT_OF_GOODS){
            return $this->returnErr("当前订单状态必须为已收货!");
        }

        $entity = array(
            'reason'=>"系统超时自动完成订单操作!",
            'order_code'=>$order['order_code'],
            'operator'=>0,
            'isauto'=>1,
            'status_type'=>'ORDER',
            'cur_status'=>$order['order_status'],
            'next_status'=>OrdersModel::ORDER_COMPLETED,
        );

        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order['order_code']))->save(array('order_status'=>OrdersModel::ORDER_COMPLETED));
        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }
        if($result == 0){
            $flag = false;
            $return = "订单ID有问题!";
        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }



        if($flag){
            return $this->returnSuc($return);
        }else{
            return $this->returnErr($return);
        }
    }


	 /**
       * 设置订单状态
       * TODO：需要记录状态变更日志
       * @param $interval 判断的间隔时间 秒 为单位
       * @return array
       */
	public function orderStatusToCancel($interval){
		$map['updatetime'] = array('lt',time()-$interval);
		$map['order_status'] = OrdersModel::ORDER_TOBE_CONFIRMED;
		$map['pay_status'] = OrdersModel::ORDER_TOBE_PAID;

        $result=apiCall(OrdersApi::QUERY,array($map,array('curpage'=>1,'size'=>20)));
        $cnt = 0;
        foreach($result['info']['list'] as $order){
            $result = $this->sysCancelOrder($order);
            if($result['status']){
                $cnt ++;
            }

        }
        return $this->returnSuc($cnt);
	}

      /**
       * 设置订单状态
       * TODO：需要记录状态变更日志
       * @param $interval 判断的间隔时间 秒 为单位
       * @return array
       */
	public function orderStatusToRecieved($interval){
		$map['updatetime'] = array('lt',time()-$interval);
		$map['order_status'] = OrdersModel::ORDER_SHIPPED;

        //每次取出满足条件的20个订单
        $result=apiCall(OrdersApi::QUERY,array($map,array('curpage'=>1,'size'=>20)));
        $cnt = 0;
        foreach($result['info']['list'] as $order){
            $result = $this->sysRecivedOrder($order);
            if($result['status']){
                $cnt ++;
            }
        }
        return $this->returnSuc($cnt);
	}

      /**
       *
       * 设置订单状态
       * TODO：需要记录状态变更日志
       * @param $interval 判断的间隔时间 秒 为单位
       *
       * @return array
       */
	public function orderStatusToCompleted($interval){
		$map['updatetime'] = array('lt',time()-$interval);
		$map['order_status'] = OrdersModel::ORDER_RECEIPT_OF_GOODS;

        //每次取出满足条件的20个订单
		$result=apiCall(OrdersApi::QUERY,array($map,array('curpage'=>1,'size'=>20)));
        addLog("OrderStatus/orderStatusToCompleted",$map,$result,"订单状态自动完成 [调试]",true);
        $cnt = 0;
        foreach($result['info']['list'] as $order){
            $result = $this->sysCompleteOrder($order);
            if($result['status']){
                $cnt ++;
            }
        }
        return $this->returnSuc($cnt);

	}
	
	/**
	 * 自动评价
	 */
	public function toAutoEvaluation($interval){
		$map['updatetime'] = array('lt',time()-$interval);
		$map['order_status'] = OrdersModel::ORDER_RECEIPT_OF_GOODS;
		$map['comment_status']=OrdersModel::ORDER_TOBE_EVALUATE;

        return $this->returnSuc(0);
//		$saveEntity = array('comment_status'=>OrdersModel::ORDER_SYSTEM_EVALUATED);
//		$result = $this->model->create($saveEntity,2);
//		if($result === false){
//			return $this->returnErr($this->model->getError());
//		}
//		$result = $this->model->where($map)->lock(true)->save();
//		if($result === FALSE){
//			$error = $this->model->getDbError();
//			return $this->returnErr($error);
//		}else{
//			return $this->returnSuc($result);
//		}
	}
	
	
	


}